package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static java.time.LocalDateTime.now;

/**
 * @Author XuFeiLong
 * @DATE: 2023/4/1
 * @TIME: 21:19
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    // collect表中要使用的数据
    private MarketCollect.Column[] channelsCollect = {MarketCollect.Column.id, MarketCollect.Column.type, MarketCollect.Column.valueId};
    // goods表中要使用的数据
    private MarketGoods.Column[] channelsGoods = {MarketGoods.Column.brief, MarketGoods.Column.name, MarketGoods.Column.picUrl, MarketGoods.Column.retailPrice};

    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketCollectMapper collectMapper;


    @Override
    public List<Map<String, Object>> list(Byte type, Integer page, Integer limit) {

        List<Map<String, Object>> result = new ArrayList<>();


        // 获取collect表中要使用的数据, 放入collectList
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.or().andTypeEqualTo(type).andDeletedEqualTo(false);
        List<MarketCollect> collectList = collectMapper.selectByExampleSelective(collectExample, channelsCollect);


        // 遍历collectList, 找到每个收藏对应的goods信息
        for (MarketCollect collect : collectList) {

            // collect表中的valueId就是goods表中的主键
            Integer valueId = collect.getValueId();
            MarketGoodsExample goodsExample = new MarketGoodsExample();
            goodsExample.or().andIdEqualTo(valueId);
            MarketGoods good = goodsMapper.selectOneByExampleSelective(goodsExample, channelsGoods);
            // 将 MarketCollect collect 和 MarketGoods good 合并成Map
            // 然后再放入结果List中
            Map<String, Object> resultMap = mergeNonNullFieldsToMap(collect, good);
            result.add(resultMap);
        }
        PageHelper.startPage(page, limit);
        return result;
    }

    /**
     * 如果collect表中有这个商品的valueId则删除,没有则添加
     *
     * @param type
     * @param valueId
     * @return 200 表示成功
     */
    @Override
    public int addordelete(byte type, Integer valueId) {


        try {
            MarketCollectExample collectExample1 = new MarketCollectExample();

            // 1--表中没有的情况, 要插入
            collectExample1.or().andValueIdEqualTo(valueId).andTypeEqualTo(type);
            MarketCollect collect = collectMapper.selectOneByExample(collectExample1);
            if (collect == null) {
                MarketCollect marketCollect = new MarketCollect();
                marketCollect.setType(type);
                marketCollect.setValueId(valueId);
                marketCollect.setAddTime(now());
                marketCollect.setUpdateTime(now());
                marketCollect.setDeleted(false);
                // 因为前端没传, 数据库中userId又不能为空, 所以都设置admin123的UserId
                marketCollect.setUserId(2);
                collectMapper.insert(marketCollect);
                return 200;

                // 2--表中有过但是已经删除过了, 相当于在垃圾桶里
                // 需要把垃圾桶里的delete设置成false
            } else if (collect.getDeleted() == true) {
                MarketCollectExample collectExample = new MarketCollectExample();
                collectExample.or().andIdEqualTo(collect.getId());
                collect.setDeleted(false);
                collect.setUpdateTime(now());
                collectMapper.updateByExampleSelective(collect,collectExample);
                return 200;
            } else {

                // 3--表中有的情况, 要删除
                Integer id = collect.getId();
                MarketCollectExample collectExample2 = new MarketCollectExample();
                collectExample2.or().andIdEqualTo(id).andDeletedEqualTo(false);

                MarketCollect marketCollect = new MarketCollect();
                marketCollect.setDeleted(true);
                collectMapper.updateByExampleSelective(marketCollect, collectExample2);
                return 200;
            }
        } catch (Exception e) {
            // 如果有异常则返回888
            e.printStackTrace();
            return 888;
        }

    }

    /**
     * 将两个对象中的非空成员变量合并在一个Map中
     *
     * @param collect
     * @param good
     * @return
     */
    private Map<String, Object> mergeNonNullFieldsToMap(MarketCollect collect, MarketGoods good) {

        HashMap<String, Object> result = new HashMap<>();

        // 获取collect中的三个变量
        Integer id = collect.getId();
        Byte type = collect.getType();
        Integer valueId = collect.getValueId();
        //--->放入result
        result.put("id", id);
        result.put("type", type);
        result.put("valueId", valueId);

        // 获取good中的四个变量
        String brief = good.getBrief();
        String name = good.getName();
        String picUrl = good.getPicUrl();
        BigDecimal retailPrice = good.getRetailPrice();
        //--->放入result
        result.put("brief", brief);
        result.put("name", name);
        result.put("picUrl", picUrl);
        result.put("retailPrice", retailPrice);

        return result;
    }


}
